﻿Imports System.ComponentModel
Imports System.Web.UI
Imports System.Drawing
Imports System.Text

Namespace Controls
    ''' <summary> 
    ''' ColorPicker 颜色拾取器控件。 
    ''' </summary> 
    <DefaultProperty("ScriptPath"), ToolboxData("<{0}:ColorPicker runat=server></{0}:ColorPicker>")> _
    Public Class ColorPicker
        Inherits WebControl
        Implements IPostBackDataHandler
        Implements INamingContainer
        '为子控件提供了一个新的命名范围,保证子控件的ID唯一性 

        ''' <summary> 
        ''' 拾取文本框 
        ''' </summary> 
        Protected ColorTextBox As New TextBox()

        ''' <summary> 
        ''' 显示图标 
        ''' </summary> 
        Protected ImgHtmlImage As New System.Web.UI.HtmlControls.HtmlImage()

        ''' <summary> 
        ''' 构造函数 
        ''' </summary> 
        Public Sub New()
            MyBase.New()
        End Sub

        ''' <summary> 
        ''' 创建子控件 
        ''' </summary> 
        Protected Overloads Overrides Sub CreateChildControls()

            ColorTextBox.Size = 8
            ColorTextBox.ID = Me.ID
            Me.Controls.Add(ColorTextBox)

            ImgHtmlImage.ID = "ColorPreview"
            ImgHtmlImage.Src = ImageUrl
            ImgHtmlImage.Attributes.Add("onclick", "IsShowColorPanel('" + Me.ColorTextBox.ClientID + "','" + Me.ImgHtmlImage.ClientID + "'," + Me.LeftOffSet + "," + Me.TopOffSet + ")")
            ImgHtmlImage.Attributes.Add("class", "img")
            ImgHtmlImage.Attributes.Add("title", "选择颜色")
            Me.Controls.Add(ImgHtmlImage)

            MyBase.CreateChildControls()
        End Sub


        ''' <summary> 
        ''' 添加属性方法 
        ''' </summary> 
        ''' <param name="key">键值,如class等</param> 
        ''' <param name="valuestr">要绑定的字符串</param> 
        Public Sub AddAttributes(ByVal key As String, ByVal valuestr As String)
            ColorTextBox.Attributes.Add(key, valuestr)
        End Sub


#Region "protected override void OnPreRender(EventArgs e)"
        ''' <summary> 
        ''' 重写<see cref="System.Web.UI.Control.OnPreRender"/>方法。 
        ''' </summary> 
        ''' <param name="e">包含事件数据的 <see cref="EventArgs"/> 对象。</param> 
        Protected Overloads Overrides Sub OnPreRender(ByVal e As EventArgs)
            Dim scriptStr As String = String.Format("<link href=""{0}"" type=""text/css"" rel=""stylesheet"">" & Chr(13) & "" & Chr(10) & "<script language=""javascript"" src=""{1}""></script>" & Chr(13) & "" & Chr(10) & "", Me.Css_Path, Me.ScriptPath)



#If NET1 Then
        If Not Page.IsClientScriptBlockRegistered("ColorPickerSet") Then 
            Page.RegisterClientScriptBlock("ColorPickerSet", scriptStr) 
        End If 
#Else
            If Not Page.ClientScript.IsClientScriptBlockRegistered("ColorPickerSet") Then
                Page.ClientScript.RegisterClientScriptBlock(Me.[GetType](), "ColorPickerSet", scriptStr)
            End If

#End If

            MyBase.OnPreRender(e)
        End Sub



#End Region

#Region "Property ImageUrl"

        ''' <summary> 
        ''' 图片路径 
        ''' </summary> 
        <Bindable(True), Category("Appearance"), DefaultValue("")> _
        Public Property ImageUrl() As String
            Get
                If MyBase.ViewState("imageurl") IsNot Nothing Then
                    Return DirectCast(MyBase.ViewState("imageurl"), String)
                Else
                    Return "../images/colorpicker.gif"
                End If
            End Get
            Set(ByVal value As String)
                MyBase.ViewState("imageurl") = value
                ImgHtmlImage.Src = value
            End Set
        End Property

#End Region

#Region "Property Text"

        ''' <summary> 
        ''' 当前选择的颜色值 
        ''' </summary> 
        <Description("当前选择的颜色值"), DefaultValue("")> _
        Public Property Text() As String
            Get
                Return ColorTextBox.Text
            End Get
            Set(ByVal value As String)
                ColorTextBox.Text = value.Trim()
            End Set
        End Property

#End Region


#Region "Property ReadOnly"

        ''' <summary> 
        ''' 是否是只读。 
        ''' </summary> 
        <Description("是否是只读"), DefaultValue(True)> _
        Public Property [ReadOnly]() As Boolean
            Get
                If Environment.Version.Major = 1 Then
                    Return ColorTextBox.[ReadOnly]
                Else
                    Return False
                End If
            End Get
            Set(ByVal value As Boolean)
                If Environment.Version.Major = 1 Then
                    ColorTextBox.[ReadOnly] = value
                End If
            End Set
        End Property

#End Region


#Region "Property ScriptPath"

        ''' <summary> 
        ''' Javascript脚本文件所在目录。 
        ''' </summary> 
        <Description("Javascript脚本文件所在目录。"), DefaultValue("./")> _
        Public Property ScriptPath() As String
            Get
                Dim obj As Object = ViewState("ScriptPath")
                Return IIf(obj Is Nothing, "../js/colorpicker.js", DirectCast(obj, String))
            End Get
            Set(ByVal value As String)
                ViewState("ScriptPath") = value
            End Set
        End Property

#End Region


#Region "Property BorderStyle"

        ''' <summary> 
        ''' 边框的样式。 
        ''' </summary> 
        <Description("边框的样式。"), Category("Appearance"), DefaultValue(""), TypeConverter(GetType(BorderStyleConverter))> _
        Public Shadows Property BorderStyle() As String
            Get
                Dim obj As Object = ViewState("BorderStyle")
                Return IIf(obj Is Nothing, "solid", DirectCast(obj, String))
            End Get
            Set(ByVal value As String)
                ViewState("BorderStyle") = value
            End Set
        End Property

#End Region


#Region "Property BorderWidth"

        ''' <summary> 
        ''' 边框的宽度。 
        ''' </summary> 
        <Description("边框的宽度。"), Category("Appearance"), DefaultValue("")> _
        Public Shadows Property BorderWidth() As String
            Get

                Dim obj As Object = ViewState("BorderWidth")
                Return IIf(obj Is Nothing, "1", DirectCast(obj, String))
            End Get
            Set(ByVal value As String)
                ViewState("BorderWidth") = value
            End Set
        End Property

#End Region

#Region "Property BorderColor"

        ''' <summary> 
        ''' 边框的颜色。 
        ''' </summary> 
        <Description("边框的颜色。"), Category("Appearance"), DefaultValue("")> _
        Public Overloads Overrides Property BorderColor() As Color
            Get
                Dim obj As Object = ViewState("BorderColor")
                Return IIf(obj Is Nothing, Color.FromArgb(153, 153, 153), DirectCast(obj, Color))
            End Get
            Set(ByVal value As Color)
                ViewState("BorderColor") = value
            End Set
        End Property
#End Region


#Region "Property CSSPATH"

        ''' <summary> 
        ''' CSS文件路径。 
        ''' </summary> 
        <Description("CSS文件路径。"), Category("Appearance"), DefaultValue("")> _
        Public Property Css_Path() As String
            Get
                Dim obj As Object = ViewState("ColorPickerCssPath")
                Return IIf(obj Is Nothing, "../styles/colorpicker.css", DirectCast(obj, String))
            End Get
            Set(ByVal value As String)
                ViewState("ColorPickerCssPath") = value
            End Set
        End Property
#End Region

        ''' <summary> 
        ''' 向上偏移量 
        ''' </summary> 
        <Description("向上偏移量"), DefaultValue(0)> _
        Public Property TopOffSet() As Single
            Get
                Dim obj As Object = ViewState("TopOffSet")
                Return IIf(obj Is Nothing, 0, CSng(obj))
            End Get
            Set(ByVal value As Single)
                ViewState("TopOffSet") = value
            End Set
        End Property

        ''' <summary> 
        ''' 向左偏移量 
        ''' </summary> 
        <Description("向左偏移量"), DefaultValue(0)> _
        Public Property LeftOffSet() As Single
            Get
                Dim obj As Object = ViewState("LeftOffSet")
                Return IIf(obj Is Nothing, 0, CSng(obj))
            End Get
            Set(ByVal value As Single)
                ViewState("LeftOffSet") = value
            End Set
        End Property


#Region "IPostBackDataHandler 成员"

        ''' <summary> 
        ''' 引发PostBack事件 
        ''' </summary> 
        Public Sub RaisePostDataChangedEvent() Implements IPostBackDataHandler.RaisePostDataChangedEvent
        End Sub

        ''' <summary> 
        ''' 加载提交信息 
        ''' </summary> 
        ''' <param name="postDataKey"></param> 
        ''' <param name="postCollection"></param> 
        ''' <returns></returns> 
        Public Function LoadPostData(ByVal postDataKey As String, ByVal postCollection As System.Collections.Specialized.NameValueCollection) As Boolean Implements IPostBackDataHandler.LoadPostData
            Dim presentValue As String = Me.ColorTextBox.Text
            Dim postedValue As String = postCollection(postDataKey)

            If Not presentValue.Equals(postedValue) Then
                '如果回发数据不等于原有数据 
                Me.ColorTextBox.Text = postedValue
                Return True
            End If
            Return False

        End Function
#End Region

        ''' <summary> 
        ''' 获取颜色面板的HTML 
        ''' </summary> 
        ''' <returns></returns> 
        Public Function ColorPickHtmlContent() As String

            Dim sb As New StringBuilder()
            sb.Append("<span id=""ColorPicker{0}"" style=""display:none; position:absolute;z-index:500;"" onmouseout=""HideColorPanel('{0}');"" onmouseover=""ShowColorPanel('{0}','{1}',{2},{3});"">")
            sb.Append("<div style=""display:block;cursor:crosshair;z-index:501"" class=""article"" >")
            sb.Append("<table border=0 cellPadding=0 cellSpacing=10 onmouseover=""ShowColorPanel('{0}','{1}',{2},{3});"">")
            sb.Append("<tbody>")
            sb.Append("<tr>")

            '输出颜色面板 
            sb.Append("<script language=""javaScript"">")
            sb.Append("WriteColorPanel('{0}','{1}',{2},{3});")
            sb.Append("</script>")

            sb.Append("</tr></tbody></table>")
            sb.Append("<table style=""font-size:12px;word-break:break-all;width:100%;border:0px"" cellPadding=0 cellSpacing=10 onmouseover=""ShowColorPanel('{0}','{1}',{2},{3});"">")
            sb.Append("<tbody>")
            sb.Append("<tr>")
            sb.Append("<td align=middle rowSpan=2>选中色彩")
            sb.Append("<table border=1 cellPadding=0 cellSpacing=0 height=30 id=ShowColor{0} width=40 bgcolor="""">")
            sb.Append("<tbody>")
            sb.Append("<tr>")
            sb.Append("<td></td></tr></tbody></table></td>")
            sb.Append("<td rowSpan=2>基色: <SPAN id=RGB{0}></SPAN><br />亮度: <SPAN id=GRAY{0}>120</SPAN><br />代码: <INPUT id=SelColor{0} size=7 value="""" border=0></TD>")
            sb.Append("<td><input type=""button"" onclick=""javascript:ColorPickerOK('{0}','{1}');"" value=""确定""></TD></TR>")
            sb.Append("<tr>")
            sb.Append("<td><input type=""button"" onclick=""javascript:document.getElementById('{0}').value='';document.getElementById('{1}').style.background='#FFFFFF';HideColorPanel('{0}');"" value=""取消""></TD>")
            sb.Append("</tr></tbody></table>")
            sb.Append("</DIV>")
            sb.Append("<iframe id=""pickcoloriframe{0}"" style=""position:absolute;z-index:102;top:-1px;width:250px;scrolling:no;height:237px;"" frameborder=""0""></iframe>")
            sb.Append("</span>")

            '初始化颜色选择器的背景色 
            sb.Append("<script language=javascript>" & Chr(13) & "" & Chr(10) & "")
            sb.Append("InitColorPicker('{1}','" + Me.Text + "');" & Chr(13) & "" & Chr(10) & "")
            sb.Append("</script>" & Chr(13) & "" & Chr(10) & "")

            Return String.Format(sb.ToString(), Me.ColorTextBox.ClientID, Me.ImgHtmlImage.ClientID, Me.LeftOffSet, Me.TopOffSet)
        End Function

        ''' <summary> 
        ''' 输出html,在浏览器中显示控件 
        ''' </summary> 
        ''' <param name="output"> 要写出到的 HTML 编写器 </param> 
        Protected Overloads Overrides Sub Render(ByVal output As HtmlTextWriter)

            If Me.HintInfo <> "" Then
                output.WriteBeginTag("span id=""" + Me.ClientID + """ onmouseover=""showhintinfo(this," + Me.HintLeftOffSet + "," + Me.HintTopOffSet + ",'" + Me.HintTitle + "','" + Me.HintInfo + "','" + Me.HintHeight + "','" + Me.HintShowType + "');"" onmouseout=""hidehintinfo();"">")
            End If

            RenderChildren(output)

            If Me.HintInfo <> "" Then
                output.WriteEndTag("span")
            End If

            output.Write(Me.ColorPickHtmlContent())

        End Sub
    End Class
End Namespace